﻿using AutoMapper;
using HuaweiCloud.Dotnet.Samples.ESports.Main.Entities;
using Newtonsoft.Json;

namespace HuaweiCloud.Dotnet.Samples.ESports.Main.Dtos
{
    public class DtoProfile : Profile
    {
        public DtoProfile()
        {
            // DeviceDto
            CreateMap<CreateDeviceDto, Device>();
            CreateMap<UpdateDeviceDto, Device>();
            CreateMap<Device, DeviceDto>();

            // BindInfoDto
            CreateMap<CreateBindInfoDto, BindInfo>();
            CreateMap<DeleteBindInfoDto, BindInfo>();

            // CommunityEventsDto
            CreateMap<CreateCommunityEventsDto, CommunityEvents>()
                .ForMember(
                dest =>
                    dest.ConditionString,
                ori => 
                    ori.MapFrom(x => JsonConvert.SerializeObject(x.EventConditions))
                );
            CreateMap<CreateCommunityEventsDto, CommunityEventsDto>();
            CreateMap<UpdateCommunityEventsDto, CommunityEvents>()
                .ForMember(
                dest =>
                    dest.ConditionString,
                ori =>
                    ori.MapFrom(x => JsonConvert.SerializeObject(x.EventConditions))
                );
            CreateMap<CommunityEvents, CommunityEventsDto>()
                .ForMember(
                dest =>
                    dest.EventConditions,
                ori => ori.MapFrom(x => JsonConvert.DeserializeObject<List<EventConditionDto>>(x.ConditionString))
                );

            // Points
            CreateMap<Points, PointsDto>();
        }
    }
}
